package com.ltv.xiangqiplugin.controllers;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.ltv.xiangqiplugin.views.ChessApp;

public class MoveListControl implements ListSelectionListener {
	private ChessApp mainApp;
	
	public MoveListControl(ChessApp mainApp) {
		this.mainApp = mainApp;
	}
	
	@Override
	public void valueChanged(ListSelectionEvent event) {
		// Fix bug
		mainApp.getBoardViewControl().setOriginPanel(null);
				
		JList theList = (JList) event.getSource();
		if (theList == null) {
			return;
		}
		
		if (event.getValueIsAdjusting() == false) {
			int index = theList.getSelectedIndex();
			mainApp.getPositionStack().setIndex(mainApp.getMoveStack().getMoves().size() - 1 - index);
			mainApp.getPositionStack().notifyObservers();
			
			mainApp.getMoveStack().setIndex(mainApp.getMoveStack().getMoves().size() - 1 - index);
		}
		
	}

}
